import pygame

from part03_飞机大战.py_05_plan_sprites import SCREEN_RECT, Brckground, Enemy, CREATE_ENEMY_USEREVENT, FRAME_PER_SEC, Hero, \
    HERO_FIRE_EVENT


class PlanGame(object):
    """
    飞机大战游戏
    """

    def __init__(self):
        print("游戏初始化")
        # 背景精灵组
        self.background_group = None
        # 敌机精灵组
        self.enemy_group = None
        # 英雄的精灵组
        self.hero = None
        self.hero_group = None
        # 创建游戏的窗口
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)
        # 创建游戏的时钟
        self.clock = pygame.time.Clock()
        # 调用私有方法, 精灵和精灵组
        self.create_sprites()
        # 设置定时器事件--创建敌机1s
        pygame.time.set_timer(CREATE_ENEMY_USEREVENT, 1000)
        # 发射子弹的定时器
        pygame.time.set_timer(HERO_FIRE_EVENT, 500)

    def start_game(self):
        print("游戏开始")
        while True:
            # 1 设置刷新频率
            self.clock.tick(FRAME_PER_SEC)
            # 2 事件监听
            self.event_handler()
            # 3碰撞检测
            self.check_collide()
            # 4更新绘制精灵
            self.update_sprites()
            # 5在屏幕显示
            pygame.display.update()

            pass

    def create_sprites(self):
        # 创建背景精灵和精灵组
        background1 = Brckground(False)
        background2 = Brckground(True)
        self.background_group = pygame.sprite.Group(background1, background2)

        # 创建敌机的精灵组
        self.enemy_group = pygame.sprite.Group()

        # 创建英雄的精灵和精灵组
        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)

        pass

    def event_handler(self):
        for event in pygame.event.get():
            # 判断是否退出游戏
            if event.type == pygame.QUIT:
                PlanGame.game_over()
            elif event.type == CREATE_ENEMY_USEREVENT:
                # print("敌机出场")
                # 创建敌机的精灵组
                enemy = Enemy()
                # 将敌机精灵添加到敌机精灵组
                self.enemy_group.add(enemy)
            # 长按不能捕获
            # elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
            #     print("向右移动...")
            elif event.type == HERO_FIRE_EVENT:
                self.hero.fire()

            # 方式二:使用键盘提供的方法获取键盘按键,按键元组
            keys_pressed = pygame.key.get_pressed()
            # 判断元组中对应的索引值
            if keys_pressed[pygame.K_LEFT]:
                self.hero.speed = -2
            elif keys_pressed[pygame.K_RIGHT]:
                self.hero.speed = 2
            else:
                self.hero.speed = 0
            pass

    def check_collide(self):
        # 子弹摧毁敌机, 检测敌机和子弹碰撞, 碰撞了, 删除两个
        pygame.sprite.groupcollide(self.enemy_group, self.hero.buttlt_group, True, True)

        # 检测敌机碰撞英雄(返回敌机的列表)
        spritecollide = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
        if len(spritecollide) > 0:
            # 英雄牺牲
            self.hero.kill()
            # 结束游戏
            PlanGame.game_over()

        pass

    def update_sprites(self):
        # 更新背景
        self.background_group.update()
        self.background_group.draw(self.screen)
        # 更新敌机
        self.enemy_group.update()
        self.enemy_group.draw(self.screen)
        # 更新英雄
        self.hero_group.update()
        self.hero_group.draw(self.screen)
        # 更新子弹的精灵组
        self.hero.buttlt_group.update()
        self.hero.buttlt_group.draw(self.screen)

        pass

    @staticmethod
    def game_over():
        print("游戏结束")
        exit()

    pass


if __name__ == '__main__':
    # 创建游戏对象
    game = PlanGame()
    # 启动游戏
    game.start_game()
    pass
